package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SchoolClasses;
import com.ruoyi.system.api.domain.SchoolStudent;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.SchoolStudentTj;
import com.ruoyi.system.service.ISchoolClassesService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.system.service.ISchoolStudentService;

import javax.servlet.http.HttpServletResponse;

/**
 * 学生所属班级Controller
 * 
 * @author ruoyi
 * @date 2022-11-19
 */
@RestController
@RequestMapping("/student")
public class SchoolStudentController extends BaseController
{
    @Autowired
    private ISchoolStudentService schoolStudentService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISchoolClassesService schoolClassesService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询学生所属班级列表
     */
    @RequiresPermissions("business:student:list")
    @GetMapping("/list")
    public TableDataInfo list(SchoolStudent schoolStudent)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            schoolStudent.setSchoolId(user.getDeptId());
        }
        startPage();
        List<SchoolStudent> list = schoolStudentService.selectSchoolStudentList(schoolStudent);
        return getDataTable(list);
    }

    @GetMapping("/listTj")
    public TableDataInfo listTj(SchoolStudent schoolStudent)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            schoolStudent.setSchoolId(user.getDeptId());
        }
        startPage();
        List<SchoolStudentTj> list = schoolStudentService.selectSchoolStudentListTj(schoolStudent);
        return getDataTable(list);
    }

    @GetMapping("/listPmTj")
    public TableDataInfo listPmTj(SchoolStudent schoolStudent)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            schoolStudent.setSchoolId(user.getDeptId());
        }
        startPage();
        List<SchoolStudentTj> list = schoolStudentService.selectSchoolStudentListPmTj(schoolStudent);
        return getDataTable(list);
    }

    /**
     * 导出学生所属班级列表
     */
    @RequiresPermissions("business:student:export")
    @Log(title = "学生所属班级", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, SchoolStudent schoolStudent)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            schoolStudent.setSchoolId(user.getDeptId());
        }
        List<SchoolStudent> list = schoolStudentService.selectSchoolStudentList(schoolStudent);
        for (SchoolStudent obj:list) {
            SysDept sysDept = sysDeptService.selectDeptById(obj.getSchoolId());
            SchoolClasses schoolClasses = schoolClassesService.selectSchoolClassesById(obj.getClassesId());
            SysUser sysUserJs = sysUserService.selectUserById(obj.getTeacherId());
            SysUser sysUserXs = sysUserService.selectUserById(obj.getStudentId());
            obj.setSchoolName(sysDept.getDeptName());
            obj.setClassesName(schoolClasses.getClassesName());
            obj.setXsUsername(sysUserXs.getUserName());
            obj.setXsPhone(sysUserXs.getPhonenumber());
            obj.setOpUsername(sysUserJs.getUserName());
            obj.setOpPhone(sysUserJs.getPhonenumber());
        }
        ExcelUtil<SchoolStudent> util = new ExcelUtil<SchoolStudent>(SchoolStudent.class);
        util.exportExcel(response, list, "学生所属班级");
    }

    /**
     * 获取学生所属班级详细信息
     */
    @RequiresPermissions("business:student:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(schoolStudentService.selectSchoolStudentById(id));
    }

    /**
     * 新增学生所属班级
     */
    @RequiresPermissions("business:student:add")
    @Log(title = "学生所属班级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SchoolStudent schoolStudent)
    {
        return toAjax(schoolStudentService.insertSchoolStudent(schoolStudent));
    }

    /**
     * 修改学生所属班级
     */
    @RequiresPermissions("business:student:edit")
    @Log(title = "学生所属班级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SchoolStudent schoolStudent)
    {
        return toAjax(schoolStudentService.updateSchoolStudent(schoolStudent));
    }

    @RequiresPermissions("business:student:editAll")
    @Log(title = "学生班级设置", businessType = BusinessType.UPDATE)
    @PutMapping("/all")
    public AjaxResult editAll(@RequestBody List<SchoolStudent> list)
    {
        return toAjax(schoolStudentService.updateSchoolStudentAll(list));
    }

    /**
     * 删除学生所属班级
     */
    @RequiresPermissions("business:student:remove")
    @Log(title = "学生所属班级", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(schoolStudentService.deleteSchoolStudentByIds(ids));
    }
}
